// pages/mine/mine.js

import {
  ajax,
  src,
  util
} from "../../utils/util.js";
const app = getApp();

var height = app.globalData.navHeight
import footer from '../../template/footer'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navH:height,
    userxinxi:"",
    touxiang:'',
    userxinxisize:"",
    customerService: "",
    total: "",
    per:'',
    isAudit:false,
  },

  toyiji: function (e) {

    var index = e.currentTarget.dataset.index
    console.log(index)
    footer.toyiji(index)
  },


  tologin:function(){
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },

  uploadMedical: function () {
    var that = this
    wx.showActionSheet({
      itemList: ['从相册选取', '拍照'],
      success(res) {
        var index = res.tapIndex;
        var dogphoto = that.data.dogphoto;
        if (index == 0) {
          let num = 1; //一次最大上传九张
          wx.chooseImage({
            count: num,
            sizeType: ['original', 'compressed'],
            sourceType: ['album'],
            success(res) {
              console.log(res)
              // tempFilePath可以作为img标签的src属性显示图片
              const tempFilePaths = res.tempFilePaths;
              console.log(tempFilePaths)
              var touxiang = tempFilePaths[0]

              that.setData({
                touxiang: touxiang
              });
              that.shangchuantp()

            }
          })
        } else if (index == 1) {
          wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['camera'],
            success(res) {
              console.log(res)
              // tempFilePath可以作为img标签的src属性显示图片
              const tempFilePaths = res.tempFilePaths;
              var touxiang = tempFilePaths[0]

              that.setData({
                touxiang: touxiang
              });
              that.shangchuantp()
            }
          })
        }

      },
      fail(res) {
        console.log(res.errMsg)
      }
    })

  },

  computerper:function(){
    // userxinxisize

    var str = this.data.userxinxisize;

    var arr = str.match(/\d+(.\d+)?/g);    // arr: ["2.75","3.65"]

    console.log(arr[0])
    str = str.replace(arr[0], "");
    console.log(str)

    if (str == 'KB'){
      var per = arr[0] / (20 * 1024 * 1024)*100
    }
    if (str == 'MB') {
      var per = arr[0] / (20 * 1024) *100
    }
    if (str == 'G') {
      var per = arr[0] / 20 *100
    }
    this.setData({
      per:per
    })

  },

  onPageScroll: function (e) {
    if (e.scrollTop < 0) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    }
  },


  shangchuantp: function () {
    var that=this
    wx.uploadFile({
      url: src + '/updateUserInfo', //仅为示例，非真实的接口地址
      filePath: that.data.touxiang,
      name: 'file',
      formData: {
        // PersonImg: this.data.touxiang,
        // 'token': wx.getStorageSync('Access_token'),
      },

      header: { 
        "Accept":'*/*',
        "Content-Type": "multipart/form-data",
        "Authorization1": 'Bearer ' + wx.getStorageSync("token")
      },

      success(res) {
        // const data = res.data
        //do something
        console.log("上传成功")
        that.getuserxinxi()
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  tohuchuan:function(){
    wx.navigateTo({
      url: '/pages/huchuan/huchuan?url=' + this.data.url,
    })
  },

  getuserxinxi:function(){
    ajax('/wxUserInfo', {
      data: {
        sessionKey: wx.getStorageSync("sessionKey"),
        encryptedData: wx.getStorageSync("encryptedData"),
        iv: wx.getStorageSync("iv"),
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'post'
    }, res => {
      console.log(res)
      this.setData({
        userxinxi: res.data.userInfo,
        userxinxisize: res.data.size,
        customerService: res.data.customerService,
        total: res.data.total,
        url: res.data.pcAddress
      })

      this.computerper()
    })
  },

  togerenxinxi:function(){
    wx.navigateTo({
      url: '/pages/xiugai/xiugai',
    })
  },

  copy: function (e) {
    var hao = e.currentTarget.dataset.hao
     var that = this;
     wx.setClipboardData({
        data: hao,
        success: function (res) {
          
        }
     });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    if(wx.getStorageSync("isAudit")=='11111'){
      this.setData({
        isAudit:false
      })
    }else{
      this.setData({
        isAudit:true
      })
    }

    this.setData({
      touxiang:""
    })
    wx.setNavigationBarColor({
      'frontColor': '#ffffff',
      'backgroundColor':'#ffffff'
    })
    this.getuserxinxi()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    // wx.stopPullDownRefresh()
  },

  onPageScroll: function (e) {
    if (e.scrollTop < 0) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    }
  }
})